
/**
 * @description: 实现以下功能
 * addCurry(1)(2)(3)(4, 15)()//25
 * @param {*} fn
 * @return {*}
 * @author: CHY
 */

function add(...args) {
    return args.reduce((a, b) => a + b)
}

function currying(fn) {
    let args = []
    return function _c(...newArgs) {
        if (newArgs.length) {
            args = [
                ...args,
                ...newArgs
            ]
            return _c
        } else {
            return fn.apply(this, args)
        }
    }
}

let addCurry = currying(add)
// 注意调用方式的变化
console.log(addCurry(1)(2)(3)(4, 15)())

/**
 * @description: 传入任意一个函数，上一个函数是下一个函数的参数，依次执行
 * @param {array} args
 * @return {*}
 * @author: CHY
 */
const pipe = (...args) => x => args.reduce(
    (outputValue, crrentFunction) => crrentFunction(outputValue), x
)
console.log(pipe(x => 1)(), pipe(x => 3)())